using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using System.Windows.Forms;
using System.Configuration;

namespace eBirdAttend
{
    public partial class UserDetails : UserControl
    {
        public string strCon = Properties.Settings.Default["ConString"].ToString();
        SqlConnection sqlCon = new SqlConnection();
        SqlCommand sqlCmd = new SqlCommand();
        String strBtnCaption = "";       

        public UserDetails()
        {
            InitializeComponent();
            sqlCon.ConnectionString = strCon;
        }

        // Function name   : btnAdd_Click
        // Description     : 
        // Return type     : void 
        // Author          : Priya Purushothaman
        // Date            : 10/17/2007
        // Modified By     : 
        // Modified On     : 
        // Purpose         : 
        // Argument        : object sender
        // Argument        : EventArgs e
        
        private void btnAdd_Click(object sender, EventArgs e)
        {  
            ClearControls();
            groupBox1.Visible = true;
            strBtnCaption = "Add";
            groupBox1.Text = "ADD AN USER";
            txtUsrId.Focus();
          
            sqlCmd.CommandText = "select max(usuid) from userdetails";
            sqlCmd.Connection = sqlCon;
            sqlCon.Open();
            SqlDataReader sqlReader;
            sqlReader = sqlCmd.ExecuteReader();
            if (sqlReader.Read())
            {
                if (sqlReader[0].ToString() == "")
                {
                }
                else
                {
                    txtUsrId.Text = Convert.ToString(Convert.ToInt32(sqlReader[0].ToString()) + 1);
                }
            }
            else
            {
                txtUsrId.Text = "1";
            }

            if (txtUsrId.Text == "")
            {
                txtUsrId.Text = "1";
            }
            txtEmpId.Focus();
            sqlCon.Close();           
        }

        private void UserDetails_Load(object sender, EventArgs e)
        {
            userDetailsTableAdapter.Fill(UserDetailsDataset1.UserDetails);
            cmbStatus.SelectedIndex = 0;
        }

        // Function name   : btnEdit_Click
        // Description     : 
        // Return type     : void 
        // Author          : Priya Purushothaman
        // Date            : 10/17/2007
        // Modified By     : 
        // Modified On     : 
        // Purpose         : 
        // Argument        : object sender
        // Argument        : EventArgs e
        
        private void btnEdit_Click(object sender, EventArgs e)
        {
            txtUsrId.Visible = true;
            strBtnCaption = "Edit";
            useridlabel.Visible = true;
            groupBox1.Text = "EDIT AN USER";
            if (userView.SelectedRows.Count == 0)
            { MessageBox.Show("Please select a row for edit"); return; }

            if ((userView.RowCount - 1) == userView.SelectedRows[0].Index)
            {
                MessageBox.Show("No content to edit");
                groupBox1.Visible = false;
                ClearControls();
                return;
            }

            groupBox1.Visible = true;           
            //MessageBox.Show(dataGridView1.SelectedRows[0].Cells[1].Value.ToString());
            txtEmpId.Text = userView.SelectedRows[0].Cells[1].Value.ToString();
            txtUsrId.Text = userView.SelectedRows[0].Cells[0].Value.ToString();
            txtLogin.Text = userView.SelectedRows[0].Cells[2].Value.ToString();
            txtPassword.Text = userView.SelectedRows[0].Cells[4].Value.ToString();
            txtRenterPwd.Text = txtPassword.Text;
            if (userView.SelectedRows[0].Cells[3].Value.ToString() == "1")
            {
                cmbStatus.SelectedIndex = 0;
            }
            else
            {
                cmbStatus.SelectedIndex = 1;
            }            
        }

        // Function name   : btnSave_Click
        // Description     : 
        // Return type     : void 
        // Author          : Priya Purushothaman
        // Date            : 10/17/2007
        // Modified By     : 
        // Modified On     : 
        // Purpose         : 
        // Argument        : object sender
        // Argument        : EventArgs e
        
        private void btnSave_Click(object sender, EventArgs e)
        {
            if (txtPassword.Text != "")
            {
                if (txtPassword.Text != txtRenterPwd.Text)
                {
                    MessageBox.Show("Passwords must match");
                    txtPassword.Focus();
                }
                else
                {
                    int strStatus = -1;

                    if (cmbStatus.SelectedIndex == 0)
                    {
                        MessageBox.Show("Select the status");
                        cmbStatus.Focus();
                        return;
                    }
                    else if (cmbStatus.SelectedIndex == 1)
                    {
                        strStatus = 1;
                    }
                    else
                    {
                        strStatus = 0;
                    }
                    try
                    {
                        //insert the new user details
                        if (strBtnCaption == "Add")
                        {
                            while (userView.Rows.Count > 1)
                                userView.Rows.RemoveAt(0);
                           
                            sqlCmd.CommandText = "insert into UserDetails values('" + txtEmpId.Text + "','" + txtLogin.Text + "','" + txtPassword.Text + "','" + strStatus + "')";
                            sqlCmd.Connection = sqlCon;
                            sqlCon.Open();
                            sqlCmd.ExecuteNonQuery();

                            userDetailsTableAdapter.Fill(UserDetailsDataset1.UserDetails);
                            MessageBox.Show("Successfuly Inserted");
                        }
                        else
                        {
                            //update the user details
                            while (userView.Rows.Count > 1)
                                userView.Rows.RemoveAt(0);
                            sqlCmd.CommandText = "update  UserDetails set UsEmpCode='" + txtEmpId.Text + "',UsLogin='" + txtLogin.Text + "',UsPassword='" + txtPassword.Text + "',UsStatus='" + strStatus + "'  where  UsUID='" + txtUsrId.Text + "'";
                            sqlCmd.Connection = sqlCon;
                            sqlCon.Open();
                            int y = sqlCmd.ExecuteNonQuery();

                            userDetailsTableAdapter.Fill(UserDetailsDataset1.UserDetails);

                            if (y == 1)
                                MessageBox.Show("Successfuly Updated");
                        }
                    }
                    catch (Exception e1)
                    {
                    }
                    finally
                    {
                        sqlCon.Close();
                        ClearControls();
                    }
                }
            }
            else
            {
                MessageBox.Show("Enter the password");
            }
        }

        // Function name   : btnDelete_Click
        // Description     : 
        // Return type     : void 
        // Author          : Priya Purushothaman
        // Date            : 10/17/2007
        // Modified By     : 
        // Modified On     : 
        // Purpose         : 
        // Argument        : object sender
        // Argument        : EventArgs e
        
        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (userView.SelectedRows.Count == 0)
            { MessageBox.Show("Please select a row to delete"); return; }

            string strDepID = userView.SelectedRows[0].Cells[0].Value.ToString();
            while (userView.Rows.Count > 1)
                userView.Rows.RemoveAt(0);
            sqlCmd.CommandText = "delete from  UserDetails where UsUID ='" + strDepID + "'";
            sqlCmd.Connection = sqlCon;
            sqlCon.Open();
            sqlCmd.ExecuteNonQuery();
            sqlCon.Close();
            userDetailsTableAdapter.Fill(UserDetailsDataset1.UserDetails);

            MessageBox.Show("Successfuly Deleted");
            ClearControls();
            groupBox1.Visible = false;
        }

        // Function name   : ClearControls
        // Description     : 
        // Return type     : void 
        // Author          : Priya Purushothaman
        // Date            : 10/18/2007
        // Modified By     : 
        // Modified On     : 
        // Purpose         : 
        
        public void ClearControls()
        {
            txtEmpId.Text = "";
            txtUsrId.Text = "";
            txtPassword.Text = "";
            txtRenterPwd.Text = "";
            txtLogin.Text = "";
            cmbStatus.SelectedIndex = 0;          
        }

        // Function name   : btnCancel_Click
        // Description     : 
        // Return type     : void 
        // Author          : Priya Purushothaman
        // Date            : 10/18/2007
        // Modified By     : 
        // Modified On     : 
        // Purpose         : 
        // Argument        : object sender
        // Argument        : EventArgs e
        
        private void btnCancel_Click(object sender, EventArgs e)
        {
            groupBox1.Visible = false;          
            ClearControls();
        }

        // Function name   : btnClose_Click
        // Description     : 
        // Return type     : void 
        // Author          : Priya Purushothaman
        // Date            : 10/19/2007
        // Modified By     : 
        // Modified On     : 
        // Purpose         : 
        // Argument        : object sender
        // Argument        : EventArgs e
        
        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void lnkLblSearch_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            SearchForm _SearchForm = new SearchForm();
            _SearchForm.ShowDialog();           
            txtEmpId.Text =  _SearchForm.strEmpCode;
        }                  
    }
}
